package com.qianli.cashmama.trade.openApi.utils.contact;

import com.alibaba.fastjson.JSON;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.DateUtil;
import com.fqgj.common.utils.HttpUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Created by Administrator on 2016/7/27.
 */
@Component
public class ContactHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(ContactHandler.class);

    /**
     * 从url中加载数据
     *
     * @param deviceContactUrl
     * @param mobile
     * @param realName
     * @return
     * @throws Exception
     */
    public static DeviceData loadDeviceContactData(String deviceContactUrl, String smsUrl, String mobile, String realName) throws Exception {
        if (StringUtils.isBlank(deviceContactUrl)) {
            return null;
        }
        // 截取url中指定字符串（包含指定字符串） 之后的数据
        String content = HttpUtil.getPage(deviceContactUrl);
        String smsData = null;
        if (StringUtils.isNotEmpty(smsUrl)) {
            smsData = HttpUtil.getPage(smsUrl);
        }

        DeviceData data = getContactByList(content, smsData, mobile);
        mobile = mobile.replaceAll("-", "").replaceAll(" ", "");
        if (mobile.startsWith("+91")) {
            mobile = mobile.substring(3);
        }
        data.setRealName(realName);
        data.setMobile(mobile);
        return data;
    }


    public static DeviceData getContactByList(String content, String smsData, String mobile) throws Exception {
        DeviceData data = JSON.parseObject(content, DeviceData.class);
        if (data.getContacts() != null) {
            data.setTotalNum(data.getContacts().size());

            for (DeviceContact contact : data.getContacts()) {
                contact.setPhone(contact.getPhone().replaceAll("-", "").replaceAll(" ", ""));
                if (contact.getPhone().startsWith("+91")) {
                    contact.setPhone(contact.getPhone().substring(3));
                }
            }
        }
        //暂时没有通话记录
//        if (data.getCalls() != null) {
//            for (DeviceCall call : data.getCalls()) {
//                call.setCallTimeDate(new Date(Long.parseLong(call.getCallTime())));
//                call.setCallTime(toDateString(call.getCallTime()));
//            }
//        }

        List<DeviceSms> smsList = data.getSmsList();
        if (StringUtils.isNotEmpty(smsData)) {
            DeviceData smsRes = JSON.parseObject(content, DeviceData.class);
            smsList = smsRes.getSmsList();
        }

        if (Objects.nonNull(smsList) && CollectionUtils.isNotEmpty(smsList)) {
            for (DeviceSms deviceSms : smsList) {
                deviceSms.setDateObj(new Date(Long.parseLong(deviceSms.getDate())));
                deviceSms.setDate(toDateString(deviceSms.getDate()));

                deviceSms.setDataSendDate(new Date(Long.parseLong(deviceSms.getDataSend())));
                deviceSms.setDataSend(toDateString(deviceSms.getDataSend()));
            }
        }
        return data;
    }

    private static String toDateString(String time) {
        if (StringUtils.isBlank(time) || time.length() <= 4) {
            return "";
        }
        Date date = new Date(Long.parseLong(time));
        return DateUtil.getDate(date, "yyyy-MM-dd HH:mm:ss");
    }


}
